// index.js
// const app = getApp()
const app = getApp()
Page({
  data: {
    roomID: '',
    template: '1v1',
    debugMode: false,
    cloudenv: 'PRO',
    evnArray: [
      { value: 'PRO', title: 'PRO' },
      { value: 'CCC', title: 'CCC' },
      { value: 'DEV', title: 'DEV' },
      { value: 'UAT', title: 'UAT' },
    ],
    headerHeight: app.globalData.headerHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    picUrl1:'',
    picUrl:''
  },

  onLoad: function() {
  },
  ocrNameInput:function(){
    this.setData({
      ocrName:e.detail.value
    })
  },
  ocrSexInput: function () {
    this.setData({
      ocrSex: e.detail.value
    })
  },
  ocrNationInput: function () {
    this.setData({
      ocrNation: e.detail.value
    })
  },
  ocrBirthInput: function () {
    this.setData({
      ocrBirth: e.detail.value
    })
  },
  ocrAddressInput: function () {
    this.setData({
      ocrAddress: e.detail.value
    })
  },

  upEnclosurerldb: function (e) {
    var that = this;
    let value = that.data.ocrIdNumber;
    let ocrName = that.data.ocrName;
    var picIndex = e.currentTarget.dataset.index;
    var attType = e.currentTarget.dataset.atttype;
    if (!value) {
      wx.showToast({
        title: '请识别或输入身份证号',
        icon: 'none',
        duration: 2000
      })
    } else if (!ocrName) {
      wx.showToast({
        title: '请识别或输入姓名',
        icon: 'none',
        duration: 2000
      })

    } else {


      var that = this;
      var sbtype = e.currentTarget.dataset.sbtype
      wx.chooseImage({
        count: 1, //最多可以选择的图片总数  
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
        sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有  
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
          var tempFilePaths = res.tempFilePaths;
          //启动上传等待中...  
          wx.showToast({
            title: '正在对比计算...',
            icon: 'loading',
            mask: true,
            duration: 20000
          })
          var uploadImgCount = 0;
          for (var i = 0, h = tempFilePaths.length; i < h; i++) {
            wx.uploadFile({
              url:  'https://www.zhaodui.com.cn/fxj380/rest/base/uploadsb',
              filePath: tempFilePaths[i],
              name: 'uploadfile_ant',
              formData: {
                sbtype: sbtype,
                filetype: 'sb',
                orderNo: '123456',
                cardid: that.data.ocrIdNumber,
                realname: that.data.ocrName
              },
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function (res) {
                uploadImgCount++;
           
                                console.log(res);
                if (sbtype == 99) {
                  var data = JSON.parse(res.data);
                  data = JSON.parse(data[0].piccontent);
                  var items = data.result.result.Similarity;
                  that.setData({
                    rldbxsd: items,
                    picUrl: JSON.parse(res.data)[0].fileUrl,
                    picId: JSON.parse(res.data)[0].fileId,
                  })
                  console.log("sbtype == 99:" + JSON.stringify(items))
                }
                if (uploadImgCount == tempFilePaths.length) {
                  wx.hideToast();
                }
              },
              fail: function (res) {
                wx.hideToast();
                wx.showModal({
                  title: '错误提示',
                  content: '对比失败',
                  showCancel: false
                })
              }
            });
          }
        }
      });
    }
  },
  upEnclosure: function (e) {
    var that = this;
    var sbtype = e.currentTarget.dataset.sbtype;
    var picIndex = e.currentTarget.dataset.index;
    var attType = e.currentTarget.dataset.atttype;
    wx.chooseImage({
      count: 1, //最多可以选择的图片总数  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 20000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url:  'https://www.zhaodui.com.cn/fxj380/rest/base/uploadsb',
            filePath: tempFilePaths[i],
            name: 'uploadfile_ant',
            formData: {
              sbtype: sbtype,
              filetype: 'sb',
              orderNo: '123456'
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              uploadImgCount++;
         
              console.log(res);
              if (sbtype == 2) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                var a = items[6].content;
                var b = a.toString();
                var c = b.substring('12')
                console.log(JSON.parse(res.data)[0].fileUrl)
                
                console.log(c)
                that.setData({
                  picUrl1: JSON.parse(res.data)[0].fileUrl,
                  picId1: JSON.parse(res.data)[0].fileId,

                  ocrName: items[1].content,
                  ocrSex: items[2].content,
                  ocrNation: items[3].content,
                  ocrBirth: items[4].content,
                  ocrAddress: items[5].content,
                  roomID: c,
                  ocrIdNumber:items[6].content,
                })
                console.log("sbtype == 2:" + JSON.stringify(items))
              } else if (sbtype == 3) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                that.setData({
                  ocrOffice: items[1].content,
                  ocrValidityPeriod: items[2].content,
                  cusIdValidfrom: items[3].content,
                  cusIdValidto: items[4].content
                })
                console.log("sbtype == 3:" + JSON.stringify(items))

              } else if (sbtype == 6) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                that.setData({
                  brand: items[5].content,
                  plateNumber: items[1].content,
                  holder: items[3].content,
                  dateIssue: items[8].content,
                  nature: items[10].content,
                  address: items[4].content,
                  vinno: items[6].content,
                  engineNumber: items[7].content,
                })
                console.log("sbtype == 6:" + JSON.stringify(items))
              } else if (sbtype == 5) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                that.setData({
                  holder1: items[2].content,
                  dateIssue1: items[8].content,
                  nature1: items[7].content,
                  address1: items[4].content,

                })
                console.log(data);
                console.log(items);
              }
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false
              })
            }
          });
        }
      }
    });
  },
  enterRoomID: function(event) {
    console.log('index enterRoomID', event)
    this.setData({
      roomID: event.detail.value,
    })
  },
  selectTemplate: function(event) {
    console.log('index selectTemplate', event)
    this.setData({
      template: event.detail.value,
    })
  },
  switchDebugMode: function(event) {
    console.log('index switchDebugMode', event)
    this.setData({
      debugMode: event.detail.value,
    })
  },
  selectEnv: function(event) {
    console.log('index switchDebugMode', event)
    this.setData({
      cloudenv: event.detail.value,
    })
  },
  enterRoom: function() {
    const roomID = this.data.roomID
    const nowTime = new Date()
    if (nowTime - this.tapTime < 1000) {
      return
    }
    if (!roomID) {
      wx.showToast({
        title: '请输入房间号',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (/^\d*$/.test(roomID) === false) {
      wx.showToast({
        title: '房间号只能为数字',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (roomID > 4294967295 || roomID < 0) {
      wx.showToast({
        title: '房间号取值范围为 1~4294967295',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    const url = `../room/room?roomID=${roomID}&template=${this.data.template}&debugMode=${this.data.debugMode}&cloudenv=${this.data.cloudenv}`
    wx.navigateTo({
      url: url,
    })
    this.setData({ 'tapTime': nowTime })
  },
  onBack: function() {
    wx.navigateBack({
      delta: 1,
    })
  },
})
